﻿Imports System.Drawing
Imports SRAGWMLib.SRAGWM

Partial Public Class WebForm1
    Inherits System.Web.UI.Page

    'declaração variaveis
    Public AuxMapa As String
    Public AS1, Pnts(5), Rng, ColumnA(1)
    Dim MagFact As Double
    Dim dx1, dy1, dx2, dy2, MSS As String

    Public objMapServerHelper As MAPSRVLib.GWMMapServerHelper

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Options()
        End If
    End Sub

    Sub Options()
        Dim Paramn As String
        If Me.dd_display.SelectedValue = 2 Then
            Paramn = "Counties"
        Else
            Paramn = "States"
        End If

        Select Case rbl_addons.SelectedValue
            Case 0
                Map(Paramn, "Rivers")
                Clean()
            Case 1
                Map(Paramn, "Interstates")
                Clean()
            Case 2
                Map(Paramn, "Cities")
                Clean()
            Case Else
                Map(Paramn, "")
                Clean()
        End Select

    End Sub

    Sub Map(ByVal Parameter As String, ByVal parameter2 As String)
        Dim BackgroundColor As Color = Color.White

        Dim database As New Database("Access.GDatabase", "C:\Warehouses\USSampleData.mdb", "", "USA")
        Dim SRAMapServer As New SRAMapServer(0, BackgroundColor)

        GWMHelper.Connection(database, SRAMapServer, "C:\Warehouses\USA.csf", objMapServerHelper)

        Coordinates()

        ' Criação do layout dos stados.
        If Displays(Parameter, parameter2) = True Then
            If Request.QueryString("mss") <> "" Then
                objMapServerHelper.LoadMSS(MSS)
            End If


            Dim Range As New Range(MagFact, 40000000, 100000)

            ' Gerar o mapa
            If Request.QueryString("mss") = "" Or Request.QueryString("x1") = 1 Then

                Dim MapDetails As New MapAtributes(Rng, 700, 450)
                GWMHelper.GenerateMap(objMapServerHelper, Range, MapDetails)

            Else

                ' Saber a 'escala' do mapa
                Dim dx As String
                Dim dy As String

                dx = CDbl(Rng(2)) - CDbl(Rng(0))
                dy = Rng(3) - Rng(1)

                Rng(0) = dx * dx1
                Rng(1) = dy * dy1
                Rng(2) = dx * dx2
                Rng(3) = dy * dy2


                Dim MapDetails As New MapAtributes(Rng, 700, 450)

                GWMHelper.GenerateMap(objMapServerHelper, Range, MapDetails)

            End If

            AuxMapa = objMapServerHelper.OutputFile
            Session("MSSFile") = objMapServerHelper.MSS
        End If
    End Sub

    Sub Coordinates()

        MSS = Request.QueryString("mss")

        'Definir e mostrar a quantidade de aumento
        If Request.QueryString("mss") = "" Or Request.QueryString("x1") = 1 Then
            MagFact = 1
            Response.Write("Sem Factor")
        Else
            'Valor de Aumento/Redução
            MagFact = 1 + CDbl(Request.QueryString("x1"))
            Response.Write("Factor: " & MagFact - 1)

            'Pedir coordendas da querystring caso existam
            dx1 = Request.QueryString("x1")
            If dx1 = "" Then dx1 = 0
            dy1 = Request.QueryString("y1")
            If dy1 = "" Then dy1 = 0
            dx2 = Request.QueryString("x2")
            If dx2 = "" Then dx2 = 0
            dy2 = Request.QueryString("y2")
            If dy2 = "" Then dy2 = 0

        End If
    End Sub

    Sub CaseAddonSelected(ByVal Add As String)
        Select Case Add
            Case "Rivers"
                ColumnA(0) = "RIVER_NAME"
                ColumnA(1) = "RIVER_TYPE"
            Case "Interstates"
                ColumnA(0) = "ID"
                ColumnA(1) = "ROUTE_NUMBER"
            Case "Cities"
                ColumnA(0) = "CITY_NAME"
                ColumnA(1) = "POP"
        End Select
    End Sub

    Function Displays(ByVal Perimeter As String, ByVal Addon As String) As Boolean

        Dim Atributes(0) As String
        Dim NumPairs As Double
        Dim FillColor As Color = Color.White
        Dim BorderColor As Color = Color.Black

        ' Array com os pares de coordenadas do polígono.  
        ' NNota: estão dados em metros e não em milhas
        Pnts(0) = -315432.33
        Pnts(1) = 8909.76
        Pnts(2) = 104033.34
        Pnts(3) = -837648.24
        Pnts(4) = 485365.78
        Pnts(5) = -219889.71

        Atributes(0) = "STATE_NAME"
        NumPairs = (Pnts.Length / 2)

        If NumPairs <> CType(NumPairs, Integer) Then
            Return False
        Else

            Dim Tooltip As String = "<%." & Atributes(0) & "%" & ">"

            Dim Le As New LegendEntry(0, 1, BorderColor, FillColor, Tooltip)
            Le.SetDataSource("States_USA", "USA", "States", Atributes)
            Le.SetFilter("", Pnts, NumPairs, 4)

            Rng = GWMHelper.DisplayFeature(objMapServerHelper, Le, Tooltip)

            Return True
        End If
    End Function

    Sub Clean()
        ' Limpar os objectos
        AS1 = Nothing
        objMapServerHelper = Nothing
    End Sub

    'Botões

    Protected Sub DropDownList(ByVal sender As Object, ByVal e As EventArgs) Handles dd_display.SelectedIndexChanged
        Options()
    End Sub

    Protected Sub RadioButtonList(ByVal sender As Object, ByVal e As EventArgs) Handles rbl_addons.SelectedIndexChanged
        Options()
    End Sub

    Protected Sub btn_less_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_less.Click

        Response.Redirect("WebForm1.aspx?x1=-0,25&x2=0,25&y1=-0,25&y2=0,25&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub btn_plus_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_plus.Click

        Response.Redirect("WebForm1.aspx?x1=0,25&x2=-0,25&y1=0,25&y2=-0,25&mss=" & Session("MSSFile"))

    End Sub

    Protected Sub img_fit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles img_fit.Click

        Response.Redirect("WebForm1.aspx?x1=1&mss=" & Session("MSSFile"))

    End Sub

End Class